package com.afteryuan.server.support;

import java.util.ArrayList;

/**
 * Created by Eclipse.
 * 公司：重庆峰禾
 * 创建者: zhangzengjun
 * 类功能描述：寻路操作
 * 创建日期: 2008-7-18
 * 创建时间: 上午10:29:44
 * @author <a href="mailto:zzjfly@126.com">$Author: yangc $</a>
 * @version $Revision: 1.6 $
 */

public class PathFinding {
	public static final int ORIENTATION_UP = 0;	//正上方
	public static final int ORIENTATION_RIGHTUP = 1;	//右上方
	public static final int ORIENTATION_RIGHT = 2;	//正右方
	public static final int ORIENTATION_RIGHTDOWN = 3;	//右下方
	public static final int ORIENTATION_DOWN = 4;	//正下方
	public static final int ORIENTATION_LEFTDOWN = 5;	//左下方
	public static final int ORIENTATION_LEFT = 6;	//正左方
	public static final int ORIENTATION_LEFTUP = 7;	//左上方
	public static final int ORIENTATION_SPEED = 10;	//步长
	public ArrayList Block = new ArrayList();	//BLOCK

	private PathFinding(){
		
	}

	public static String pathFind(int startx,int starty,int endx,int endy){
		ArrayList vcpath = new ArrayList();
		String path = startx+"#"+starty+"#";

		vcpath.add(path);
		vcpath.add(startx);
		vcpath.add(starty);
		vcpath.add(endx);
		vcpath.add(endy);

		vcpath = findPath(vcpath);
		return vcpath.get(0).toString();
	}
	
	public static String pathFind(int startx,int starty,int endx,int endy,int delayTime){
		ArrayList vcpath = new ArrayList();
		String path = startx+"#"+starty+"#"+delayTime+"#";

		vcpath.add(path);
		vcpath.add(startx);
		vcpath.add(starty);
		vcpath.add(endx);
		vcpath.add(endy);

		vcpath = findPath(vcpath);
		return vcpath.get(0).toString();
	}
	
	private static ArrayList findPath(ArrayList vector){
		String path = vector.get(0).toString();
    	int startx = Integer.parseInt(vector.get(1).toString());
    	int starty = Integer.parseInt(vector.get(2).toString());  
    	int endx = Integer.parseInt(vector.get(3).toString());
    	int endy = Integer.parseInt(vector.get(4).toString());

        int dx = endx-startx;
        int dy = endy-starty;

		//System.out.println(dx+"::::"+dy);
        //判断正上方
		if ((dx==0)&&(dy<=-1)){
			starty = starty-1;
			path += PathFinding.ORIENTATION_UP;
		}
        
        //判断右上方
		if ((dx>=1)&&(dy<=-1)){
			startx = startx+1;
			starty = starty-1;
			path += PathFinding.ORIENTATION_RIGHTUP;
		}
        
        //判断正右方
		if ((dx>=1)&&(dy==0)){
			startx = startx+1;
			path += PathFinding.ORIENTATION_RIGHT;
		}
        
        //判断右下方
		if ((dx>=1)&&(dy>=1)){
			startx = startx+1;
			starty = starty+1;
			path += PathFinding.ORIENTATION_RIGHTDOWN;
		}
        
        //判断正下方
		if ((dx==0)&&(dy>=1)){
			starty = starty+1;
			path += PathFinding.ORIENTATION_DOWN;
		}
        
        //判断左下方
		if ((dx<=-1)&&(dy>=1)){
			startx = startx-1;
			starty = starty+1;
			path += PathFinding.ORIENTATION_LEFTDOWN;
		}
        
        //判断正左方
		if ((dx<=-1)&&(dy==0)){
			startx = startx-1;
			path += PathFinding.ORIENTATION_LEFT;
		}
        
        //判断左上方
		if ((dx<=-1)&&(dy<=-1)){
			startx = startx-1;
			starty = starty-1;
			path += PathFinding.ORIENTATION_LEFTUP;
		}
		vector.add(0,path);
		vector.add(1,startx);
		vector.add(2,starty);
		vector.add(3,endx);
		vector.add(4,endy);
		if(!(dx==0&&dy==0)){
				vector = findPath(vector);
		}
		return vector;
	}
	
	public static int ConvertNumber(int oranum){
		int num=oranum;
		return num;
	}
	
	
	public static void main(String[] args) { 
		long startTime = System.currentTimeMillis();
		PathFinding test = new PathFinding(); 
		String result = test.pathFind(1000, 1000, 35, 358); 
		System.out.println("path="+result); 
		System.out.println(System.currentTimeMillis()-startTime);

		} 
}
